//
//  LoginController.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "LoginController.h"

@interface LoginController ()
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgTop;
@property (weak, nonatomic) IBOutlet UIButton *youkeBtn;


@end

@implementation LoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"登录"];
    self.imgTop.constant = 30 + NavigationContentTopConstant;
    [self.loginBtn setCorner:self.loginBtn.frame.size.height/2];
    [self.youkeBtn setCorner:self.youkeBtn.frame.size.height/2];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)loginClick:(id)sender {
    
    weakify(self);
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:UMSocialPlatformType_WechatSession currentViewController:nil completion:^(id result, NSError *error) {
        if (error) {
            [QMUITips showError:@"登录失败"];
            CLIENT.isLogin = NO;
            [UserDefaults setAutoLogin:NO];
        } else {
            UMSocialUserInfoResponse *resp = result;
            
            [self wxLoginWithUid:resp.uid img:resp.iconurl sex:resp.unionGender nickName:resp.name Success:^{
                strongify(self);
                CLIENT.isLogin = YES;
                [UserDefaults setAutoLogin:YES];
                [UserDefaults setUserImage:resp.iconurl];
                [UserDefaults setUserRealName:resp.name];
                [UserDefaults setUserIntroduce:@"留光，一抹时光 一抹情"];
                [QMUITips showSucceed:@"登录成功"];
                
                [BANetManager sharedBANetManager].httpHeaderFieldDictionary = @{@"token":[UserDefaults getAccessToken] ? [UserDefaults getAccessToken] : @"",@"system":@"1",@"version": [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]};
                
                [self dismissViewControllerAnimated:YES completion:nil];
            } failed:^(NSString * _Nonnull error) {
                CLIENT.isLogin = NO;
                [UserDefaults setAutoLogin:NO];
                [QMUITips showError:error];
            }];
        }
    }];
    
}

- (IBAction)youkeClick:(id)sender {
    
    [self iphoneLoginWithUid:[self getUUID] Success:^{
        
        CLIENT.isLogin = YES;
        [UserDefaults setAutoLogin:YES];
        [UserDefaults setUserIntroduce:@"留光，一抹时光 一抹情"];
        [QMUITips showSucceed:@"登录成功"];
        [BANetManager sharedBANetManager].httpHeaderFieldDictionary = @{@"token":[UserDefaults getAccessToken] ? [UserDefaults getAccessToken] : @"",@"system":@"1",@"version": [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]};
        [self dismissViewControllerAnimated:YES completion:nil];
        
    } failed:^(NSString * _Nonnull error) {
        [QMUITips showWithText:error];
    }];
    
}

// 普通的获取UUID的方法
- (NSString *)getUUID {
    CFUUIDRef puuid = CFUUIDCreate( nil );
    CFStringRef uuidString = CFUUIDCreateString(nil, puuid);
    NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy( NULL, uuidString));
    return result;
}

@end
